<?php
include("header.inc.php");
include 'auth.inc.php';
?>

<?php

/* Connect to database */
include("db.inc.php");
$con = mysql_connect(MYSQL_HOST,MYSQL_USER,MYSQL_PASSWORD);

if (!$con) {
         die ('Error: Could not connect to mySQL server');
}
$mysql = MYSQL_DB;
if (!mysql_select_db($mysql)) {
        die ('Error: Could not connect to mySQL database');
}


/* Get table name from URL */
$tableName = mysql_real_escape_string($_GET["data"]);

/* Make sure table exist in database */
require("editData/table_exist.inc.php");

echo "<h1>Edit Data</h1>";


// If table exists 
if ($tableExists) { //START MASTER IF

echo "<h2>$tableName</h2>\n";

/* Get values from POST for Edit - To put values in Input Form */
if (count($_POST)==1 && $_POST['Submit']=='' &&
	$_POST['Selected']=='' &&
	$_POST['DeleteSelected']!='Delete Selected') {
       include("editData/get_edit_value.inc.php");
echo '<p>Editing Record...</p>';
/* The reason for the heavy checks around this include is only really
    to make sure that DeleteSelected is not called when we use the while
    loop to go through $_POST--because we need the while loop to DELETE values
    and if we use it here, that messes up DELETE's while loop--which is only
    called if DeleteSelected=='Delete Selected */

/* The other checks are supposed to save time by not including it */
}

/* Get field names or/and record values for input form table */
include("editData/input_table_fields.inc.php");

/* Parse values and replace bad characters from form input*/
if ($_POST['Submit']!="") {
        include("editData/parse_form_input.inc.php");
}

/* Check that the user has the correct access level */
if (count($_POST) && $_SESSION['access_level'] < 2 && ($_POST['Submit']=='Add Data' || $_POST['Submit']=='Make Copy' || $_POST['DeleteSelected']=='Delete Selected' || $_POST['Submit']=='Update Data')) {
	include("editData/add_changes.inc.php");

/* INSERT VALUES INTO DATABASE */

} else if (count($_POST)!=0 && $_POST['Submit']=='Add Data' || $_POST['Submit']=='Make Copy') {
        include("editData/insert_data.inc.php");
}

/* DELETE VALUES FROM DATABASE */

else if (count($_POST)!=0 && $_POST['DeleteSelected']=='Delete Selected') {
        include("editData/delete_data.inc.php");
}

/* UPDATING VALUES IN DATABASE */

else if (count($_POST)!=0 && $_POST['Submit']=='Update Data') {
        include("editData/update_data.inc.php");
}


/* INPUT FORMS */

/* Get input form type depending if the switch between types has been made */
include("editData/det_input_type.inc.php");


echo "<form action='$editData_link?data=$tableName&input=" . $_GET['input'] ."' method='post'>\n";

/* Print sub-navigation (type depends if we're editing or not) */
include("editData/print_sub_nav.inc.php");


/* Get possible foreign values */
include("editData/get_foreign_values.inc.php");


/* Calculate the ID value that should be printed */
include("editData/calc_id.inc.php");

/* INPUT TABLE: Plain text input */

if ($_GET["input"]=="plain") {
	include("editData/plain_text_input_table.inc.php");

/* INPUT TABLE: Form input */

} else {
	include("editData/form_input_table.inc.php");

}
/* Message about not_null fields ($not_null is set by plain_text_input_table or form_input_table */
if ($not_null_exist || $mult_key_exist || $unique_exist) {
	echo "<p>";
	if ($not_null_exist) {
		echo "Fields with a * are required. ";
	}
	if ($mult_key_exist) {
		echo "Fields with a ~ must match a value from another table. ";
	}
	if ($unique_exist) {
		echo "Fields with a ` must be unique with other values in this field. ";
	}
	echo "</p>\n";
	
} 

if ($edit_selected!='' || $_POST['Submit']=='Plain Text Edit' || $_POST['Submit']=='Form Edit') {
	echo "<p><input name='Submit' value='Update Data' type='Submit' />\n";
	echo "<input name='Submit' value='Make Copy' type='Submit' /></p>\n";
} else {
	echo "<p><input name='Submit' value='Add Data' type='Submit' /></p>\n";
}
echo "</form>\n";



echo "\n\n\n\n\n\n";


/* GET INFO FROM DATABASE AND PUT IN DISPLAY TABLE*/

echo "<form action='$editData_link?data=$tableName&input=" . $_GET['input'] ."' method='post'>\n";
include("editData/display_table.inc.php");
echo "</form>\n";


// Else table doesn't exist 
} else { // END MASTER IF 
	include ("editData/table_not_exist.inc.php");
}

/* CLOSE CONNECTION */
mysql_free_result($result);
mysql_close($con);

?>
<?php
include("footer.inc.php");
?>
